package com.windowes.weichang.service;

import com.windowes.weichang.common.ServerResponse;
import com.windowes.weichang.dao.CommunityMapper;
import com.windowes.weichang.pojo.Community;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

/***
 *
 * @author: yangqing
 * @email: qing.yang@bangdao-tech.com
 * @createTime: 2018/6/29 下午5:12
 * @description:
 *
 **/
@Service
public class CommunityService {


    @Autowired
    CommunityMapper communityMapper;

    /***
     *
     * @param community
     * @return
     */
    public ServerResponse addOrUpdate(Community community) {
        if (StringUtils.isEmpty(community.getName())) {
            return ServerResponse.createByErrorMessage("请提交小区名称!");
        }

        if (community.getId() == null) {
            communityMapper.insertSelective(community);
        } else {
            communityMapper.updateByPrimaryKeySelective(community);
        }
        return ServerResponse.createBySuccessMessage("操作成功!");
    }

    public ServerResponse list(Integer id) {
        if (id == null) {
            return ServerResponse.createByErrorMessage("请提交店铺id!");
        }
        List<Community> communities = communityMapper.selectByShopId(id);
        if (communities == null || communities.isEmpty()) {
            return ServerResponse.createByErrorMessage("未找到数据!");
        }
        return ServerResponse.createBySuccess(communities);
    }


    public ServerResponse delete(Integer id) {
        communityMapper.deleteByPrimaryKey(id);
        return ServerResponse.createBySuccessMessage("操作成功!");
    }

    public ServerResponse item(Integer id) {
        if (id == null) {
            return ServerResponse.createByErrorMessage("请提交id!");
        }
        Community community = communityMapper.selectByPrimaryKey(id);
        return ServerResponse.createBySuccess(community);
    }
}
